package com.loongtech.bi.action.ios;

import com.loongtech.bi.action.BaseBiSearchAction;
import com.loongtech.bi.manager.ios.IOSBillDataManager;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.Session;
import com.loongtech.bi.support.chart.ChartUIData;
import com.loongtech.core.util.NumberUtil;
import com.loongtech.core.util.Time;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * IOS账单管理--iOS账期数据
 * @author Marin
 * @date 2015年09月17日
 */
@Controller("iosBillDataManager")
@Scope("view")
public class IOSBillDataManagerAction extends BaseBiSearchAction {
	private static final long serialVersionUID = 1L;
	private String dateRadio = "7";

	private List<String> productids;  // 所有产品
	private List<String> dates;  // 所有日期
    private Map<String, IOSBillDataManager.BillData> datasByDate;
    private ChartUIData chartByDate;

    private List<IOSBillDataManager.BillAllData> datasByAll;
    private ChartUIData chartByAll;

    private List<IOSBillDataManager.BillMonthData> datasByMonth;
    private ChartUIData chartByMonth;

    private Date monthStart = new Date(Time.getBeginningOfDay(Time.currentTime() - 60 * Time.DAY));
    private Date monthEnd = new Date(Time.getBeginningOfDay(Time.currentTime() - 30 * Time.DAY));

    @Autowired
    private IOSBillDataManager iOSBillDataManager;
	@Autowired
	private SysLogManager sysLogManager;
	@Autowired
	private Session session;
	
	@PostConstruct
	public void init() {
		search();
		searchMonth();
	}
	
	/**
	 * 切换天数
	 */
	public void radioChange() {
	}
	
	public void search() {
        //不是自定义时间
		if(!"0".equals(dateRadio)) {
			dateStart = new Date(Time.getBeginningOfDay(Time.currentTime() - NumberUtil.parseInt(dateRadio) * Time.DAY));
			dateEnd = new Date(Time.getBeginningOfDay(Time.currentTime() - Time.DAY));
		}
		
		sysLogManager.addLog(session.getUserName(), "IOS账单管理--iOS账期数据");

        productids = new ArrayList<>();
        dates = new ArrayList<>();
        datasByDate = iOSBillDataManager.getBillDateDatas(dateStart, dateEnd, productids, dates);

        datasByAll = iOSBillDataManager.getBillAllDatas(dateStart, dateEnd);
    }

    public void searchMonth() {
	    datasByMonth = iOSBillDataManager.getBillMonthDatas(monthStart, monthEnd);
    }

    public IOSBillDataManager.BillData getBillData(String dateStr, String productid, boolean isAll) {
        return isAll ?  datasByDate.get("all-" + productid) : datasByDate.get(dateStr + "-" + productid);
    }

	@Override
	protected String csvContent() {
		return "";
	}

	@Override
    protected String csvFileName() {
	    return "";
    }


	public String getDateRadio() {
		return dateRadio;
	}

	public void setDateRadio(String dateRadio) {
		this.dateRadio = dateRadio;
	}

    public Map<String, IOSBillDataManager.BillData> getDatasByDate() {
        return datasByDate;
    }

    public void setDatasByDate(Map<String, IOSBillDataManager.BillData> datasByDate) {
        this.datasByDate = datasByDate;
    }

    public ChartUIData getChartByDate() {
        return chartByDate;
    }

    public void setChartByDate(ChartUIData chartByDate) {
        this.chartByDate = chartByDate;
    }

    public List<String> getProductids() {
        return productids;
    }

    public void setProductids(List<String> productids) {
        this.productids = productids;
    }

    public List<String> getDates() {
        return dates;
    }

    public void setDates(List<String> dates) {
        this.dates = dates;
    }

    public List<IOSBillDataManager.BillAllData> getDatasByAll() {
        return datasByAll;
    }

    public void setDatasByAll(List<IOSBillDataManager.BillAllData> datasByAll) {
        this.datasByAll = datasByAll;
    }

    public ChartUIData getChartByAll() {
        return chartByAll;
    }

    public void setChartByAll(ChartUIData chartByAll) {
        this.chartByAll = chartByAll;
    }

    public List<IOSBillDataManager.BillMonthData> getDatasByMonth() {
        return datasByMonth;
    }

    public void setDatasByMonth(List<IOSBillDataManager.BillMonthData> datasByMonth) {
        this.datasByMonth = datasByMonth;
    }

    public ChartUIData getChartByMonth() {
        return chartByMonth;
    }

    public void setChartByMonth(ChartUIData chartByMonth) {
        this.chartByMonth = chartByMonth;
    }

    public Date getMonthStart() {
        return monthStart;
    }

    public void setMonthStart(Date monthStart) {
        this.monthStart = monthStart;
    }

    public Date getMonthEnd() {
        return monthEnd;
    }

    public void setMonthEnd(Date monthEnd) {
        this.monthEnd = monthEnd;
    }
}
